package dongshi.daddy.huohuo;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.common.base.Splitter;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.collect.*;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class GuavaTest {

    public static void main(String[] args) throws Exception {
        // cacheTest();
        // eventBusTest();
//        testMap();
//        testBiMap();
        other();
        System.in.read();
    }

    private static void testBiMap() {
        BiMap<String, Integer> words = HashBiMap.create();
        words.put("First", 1);
        words.put("Second", 2);
        words.put("Third", 3);

        System.out.println(words.get("Second").intValue());
        System.out.println(words.inverse().get(3));

    }

    private static void other() {
        // list转逗号分割字符串
        List<String> lists = Lists.newArrayList("a", "b", "g", "8", "9");
        String result = Joiner.on(",").join(lists);
        System.out.println(result);

        String test = "34344,,,34,34,哈哈";
        // string 逗号分割 转list
        lists = Splitter.on(",").splitToList(test);
        // list 转 map 并设置value值
        Map<String, String> map1 = Maps.toMap(lists.listIterator(), a -> a + "-value");
        System.out.println(JSON.toJSONString(map1));

        // 将一个list 分割为多个list的集合，类似分区操作
        List<Integer> list = Lists.newArrayList(4, 2, 3, 5, 1, 2, 2, 7, 6);
        List<List<Integer>> list1111 = Lists.partition(list, 3);
        System.out.println(list1111);

        // 创建不可变集合
        ImmutableList<String> iList = ImmutableList.of("a", "b", "c");
        ImmutableSet<String> iSet = ImmutableSet.of("e1", "e2");
        ImmutableMap<String, String> iMap = ImmutableMap.of("k1", "v1", "k2", "v2");

        // map转换为特定规则的字符串
        Map<String, Integer> map = Maps.newHashMap();
        map.put("xiaoming", 12);
        map.put("xiaohong",13);
        String result111 = Joiner.on(",").withKeyValueSeparator("=").join(map);
// result为 xiaoming=12,xiaohong=13
        System.out.println(result111);

        // string转换为map
        String str = "xiaoming=11,xiaohong=23";
        Map<String,String> map2222 = Splitter.on(",").withKeyValueSeparator("=").split(str);
        System.out.println(map2222);


        // 判断是整数(断言)
        int count = -9;
        Preconditions.checkArgument(count > 0, "must be positive: %s", count);
    }

    private static void testMap() {
        Multimap<Integer, Integer> bMultimap = ArrayListMultimap.create();

        List<Integer> list = Lists.newArrayList(4, 2, 3, 5, 1, 2, 2, 7, 6);
        list.forEach(v -> bMultimap.put(v, v + 1));
        System.out.println(JSON.toJSONString(bMultimap));
    }

    private static void eventBusTest() {

    }

    private static void cacheTest() {
        Cache cache = CacheBuilder.newBuilder()
                .maximumSize(2)
                .expireAfterWrite(3, TimeUnit.SECONDS)
                .removalListener(removalNotification -> System.out.println(removalNotification.getValue() + " 被移除了！！！"))
                .build();
        cache.put("K1", "V1");
        cache.put("K2", "V2");
        cache.put("K3", "V3"); // 超出最大容量，会导致k1被删除
        cache.invalidate("K3"); // 手动删除
    }
}
